package com.kin.controller;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kin.pojo.User;
import com.kin.service.IUserService;
import com.kin.tools.IToolsService;

/**
 * 用户Controller
 * @author lauvinson
 * @time 2017年7月14日09:41:56
 */
@Controller("userController")
@SessionAttributes({"User","groupid","username","userid"})
public class UserController {

	/** 成功状态  */
	public final static String STATUS_SUCCESS = "{'status':'success'}";
	/** 失败状态 */
	public final static String STATUS_FAIL = "{'status':'fail'}";
	/** 确认状态 */
	public final static String STATUS_TRUE = "{'status':'true'}";
	/** 否认状态 */
	public final static String STATUS_FALSE = "{'status':'false'}";
	/** 错误状态 */
	public final static String STATUS_ERROR = "{'status':'error'}";
	@Resource
	private PageJumpController pageJumpController;//页面转向Service
	
	@Resource
	private IUserService userService;//用户Service
	
	@Resource
	private IToolsService toolsService;//工具Service
	
	
	/**
	 * 用户登录操作(POST)
	 * @param login_info(username,password)
	 * @param model Model
	 * @return status:success，status:fail,status:error
	 * @throws IOException 异常
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject login(@RequestBody JSONObject login_info,Model model) throws IOException
	{
		boolean validate =false;
		try
		{
		validate = toolsService.vilidateLoginInfo(login_info.getString("username"),login_info.getString("password"));
		}
		catch(NullPointerException e)
		{return toolsService.stringToJsonObject(STATUS_FAIL);}
		
		if(validate==true)
		{
			User user = userService.getUserByUsernameAndPassword(login_info.getString("username"),login_info.getString("password"));
			
			if(user != null)
			{
				model.addAttribute("User", JSON.toJSONString(user));
				model.addAttribute("userid",user.getId());
				model.addAttribute("groupid",user.getGroupid());
				model.addAttribute("username",user.getUsername());
			}
			return user!= null? toolsService.stringToJsonObject(STATUS_SUCCESS): toolsService.stringToJsonObject(STATUS_FAIL);
		}
		else
		{
			return toolsService.stringToJsonObject(STATUS_ERROR);
		}
	}
	
	
	/**
	 * 注销(POST)
	 * @param user Session.key
	 * @param sessionStatus Session.status
	 * @return status:success，status:fail
	 */
	@RequestMapping("/signOut")
	@ResponseBody
	public JSONObject login_out(@ModelAttribute("User") String user,SessionStatus sessionStatus)
	{     
        try
        {
        	if(user.equals(STATUS_FALSE))
        	{
        		sessionStatus.setComplete();
        		return toolsService.stringToJsonObject(STATUS_FAIL);
        	}
        	else
        	{
        		sessionStatus.setComplete();
        		return toolsService.stringToJsonObject(STATUS_SUCCESS);
        	}
        }catch(Exception e)
        {
        	e.printStackTrace();
        	return toolsService.stringToJsonObject(STATUS_FAIL);
        } 
    }  
	
	
	/**
	 * 获取登录状态(POST)
	 * @param user Session.key
	 * @return status:true，status:false
	 */
	@RequestMapping(value="/getLoginStatus",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getLoginStatus(@ModelAttribute("User") String user){
		  if(!user.equals(STATUS_FALSE))
		  {
			  return toolsService.stringToJsonObject(STATUS_TRUE);
		  }
		  else
		  {
			  
			  return toolsService.stringToJsonObject(STATUS_FALSE);
		  }
	}

	/**
	 * 默认登录状态
	 * @return susccess:false
	 */
	@ModelAttribute("User")
	public String defaultUserStatus()
	{
		return STATUS_FALSE;
	}
	
	/**
	 * 获取已登录用户的信息(POST)
	 * @param user Session.key
	 * @return json(user)，status:fail
	 */
	@RequestMapping(value="/getLoginInfo",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getLoginInfo(@ModelAttribute("User") String user)
	{
		if(!user.equals(STATUS_FALSE))
		{
			return toolsService.stringToJsonObject(user);
		}
		else
		{
			return toolsService.stringToJsonObject(STATUS_FAIL);
		}
	}
	
}
